<template>
  <div>
    <div style="text-align:left">
      患者信息：
      <el-tag>姓名：{{patient.real_name}}</el-tag>
      <el-tag>病历号：{{patient.case_number}}</el-tag>
      <el-tag>年龄：{{patient.age}}</el-tag>
      <el-tag>性别：{{patient.gender}}</el-tag>
    </div>
    <el-divider></el-divider>
    <div style="font-size:20px;text-align:left">
      <i class="el-icon-document-checked">检查申请</i>
    </div>
    <el-divider></el-divider>
    
    <div style="display:flex;">
    <el-button type="primary" disabled icon="el-icon-price-tag">项目金额：{{total_price}}元</el-button>
    </div>
    <el-table :data="check_request_table" style="width:80%" @selection-change="checkSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="tech_code" label="检查编码" width="180"></el-table-column>
        <el-table-column prop="tech_name" label="检查名称" ></el-table-column>
        <el-table-column prop="tech_format" label="检查规格" width="100"></el-table-column>
        <el-table-column prop="tech_price" label="单价" width="100"></el-table-column>
        <el-table-column prop="price_type" label="费用分类" width="100"></el-table-column>
        <el-table-column width="180">
          <template slot="header" slot-scope="scope">
            <el-button type="text" @click="delCheck()">删除</el-button>
            <el-button type="text" @click="checkDialogTableVisible=true">增加</el-button>
          </template>
        </el-table-column>
    </el-table>
    <el-descriptions title="医嘱" :column="1" border style="width:80%;margin-top:20px">
      <el-descriptions-item label="目的要求：" :labelStyle="{'width':'120px'}">
        <el-input type="textarea" :rows="4" v-model="check_info" placeholder="请输入检验目的要求"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="检验部位：" :labelStyle="{'width':'120px'}">
        <el-input type="textarea" :rows="2" v-model="check_position" placeholder="请输入检验部位"></el-input>
      </el-descriptions-item>
      <el-descriptions-item label="备注：" :labelStyle="{'width':'120px'}">
        <el-input type="textarea" :rows="4" v-model="check_remark" placeholder="请输入检验事项"></el-input>
      </el-descriptions-item>
    </el-descriptions>
    <!-- 检查项目选择对话框 -->
    <el-dialog title="添加检查申请" :visible.sync="checkDialogTableVisible">
      <div>
        <el-tag style="width:20%">检查编码：</el-tag><el-input style="width:80%" v-model="checkRequest.tech_code" placeholder="请输入检查编码"></el-input>
      </div>
      <div>
        <el-tag style="width:20%">检查名称：</el-tag><el-input style="width:80%" v-model="checkRequest.tech_name" placeholder="请输入检查名称"></el-input>
      </div>
      <div style="float:right">
        <el-button @click="searchCheck()">搜索</el-button>
      </div>
      <el-table :data="search_check" @selection-change="checkSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column property="tech_code" label="检查编码" width="150"></el-table-column>
        <el-table-column property="tech_name" label="检查名称" ></el-table-column>
        <el-table-column prop="tech_format" label="检查规格" width="100"></el-table-column>
        <el-table-column prop="tech_price" label="单价" width="100"></el-table-column>
        <el-table-column prop="price_type" label="费用分类" width="100"></el-table-column>
      </el-table>
      <div>
        <el-button type="primary" @click="addSelectionCheck()">添加</el-button>
        <el-button type="info" @click="checkDialogTableVisible=false">关闭</el-button>
      </div>
    </el-dialog>

    <el-divider></el-divider>
    <div style="text-align:left;margin-top:20px">
        <el-button type="primary" @click="addCheckRequest()">申请提交</el-button>
        <el-button type="primary" @click="clearCheckRequest()">清空表格</el-button>
    </div>
    
  </div>
</template>
<script>
import {isNullMessage} from "../../../util/util"
import qs from 'qs'
export default {
  methods:{
    //提交检查申请
    addCheckRequest(){

      this.submit_check_info['register_id'] = this.patient.id;

      let temp = [];
      for(let i=0;i<this.check_request_table.length;i++){
        temp.push(this.check_request_table[i].id);
      }
      this.submit_check_info['medical_technology_ids'] = JSON.stringify(temp);

      this.submit_check_info['check_info'] = this.check_info;
      this.submit_check_info['check_position'] = this.check_position;
      this.submit_check_info['check_remark'] = this.check_remark;

      this.$http.post("http://localhost:8092/addCheckRequest",qs.stringify(this.submit_check_info)).then(
        (resp)=>{
          this.$message({
            message:resp.data.msg,
            type:"success"
            });
        }
      )
    },
    //清空检查申请信息
    clearCheckRequest(){
      this.check_request_table = [];
      this.check_info = '';
      this.check_position='';
      this.check_remark='';
      this.total_price = 0;
    },
    //复选框点击
    checkSelectionChange(selVal){
      this.temp_check = selVal;
    },
    //添加
    addSelectionCheck(){
      if(this.temp_check!=null && this.temp_check.length>0){
        for(let i=0;i<this.temp_check.length;i++){
          this.check_request_table.push(this.temp_check[i]);
        }
      }
      this.checkRequest={};
      this.search_check=[];
      
      this.math_total_price();
    },
    //计算检查项目总金额
    math_total_price(){
      this.total_price = 0;
      for(let i=0;i<this.check_request_table.length;i++){
        this.total_price = parseFloat(this.check_request_table[i].tech_price) + this.total_price;
      }
      this.total_price = this.total_price.toFixed(2);
    },
    //删除
    delCheck(){
      for(let i=0;i<this.temp_check.length;i++){
        for(let j=0;j<this.check_request_table.length;j++){
          if(this.check_request_table[j].tech_name === this.temp_check[i].tech_name){
            this.check_request_table.splice(j,1);
            j--;
          }
        }
      }
      this.math_total_price();
    },
    //搜索检查信息
    searchCheck(){
      this.$http.post("http://localhost:8092/getCheck",qs.stringify(this.checkRequest)).then(
        (resp)=>{
          this.search_check = resp.data;
        }
      )
    }
  },
  data(){
    return{
      submit_check_info:{},//保存提交信息
      check_info:'',
      check_position:'',
      check_remark:'',
      total_price:0,//申请检查项目总金额
      temp_check:[],
      checkDialogTableVisible:false,//设置检查项目选择对话框是否显示
      patient:{}, //患者信息
      check_request_table:[],//检查申请项
      checkRequest:{},//检查项目选择对话框提交搜索数据
      search_check:[],//table的检查数据
    }    
  },
  mounted:function() {
    this.patient = JSON.parse(sessionStorage.getItem("record_now_patient"));
    if(isNullMessage(this,this.patient,'请先选择患者')){
      this.patient={
        real_name:'',
        case_number:'',
        age:'',
        gender:''
      }
    }
  },
}
</script>
